package com.wangsd.common.basicbean;

import com.wangsd.common.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
@ApiModel(description = "通用响应返回对象")
public class Result<T> {
    //状态码
    @ApiModelProperty(value = "结果代码", position = 1)
    private int code;
    //消息
    @ApiModelProperty(value = "信息", position = 2)
    private String message;
    //额外的内容
    @ApiModelProperty(value = "内容", position = 3)
    private T result;

    public Result(ResultCode resultCode) {
        this.code = resultCode.getCode();
        this.message = resultCode.getMessage();
    }

    /**
     * 响应异常信息
     */
    public static Result message(ResultCode resultCode) {
        return new Result(resultCode);
    }


    public Result(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public Result(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.result = data;
    }

    public static Result success() {
        return new Result(200, "操作成功", null);
    }

    public static Result success(Object data) {
        return new Result(200, "操作成功", data);
    }

    public static Result fail(String message) {
        return new Result(400, message, null);
    }

    public static Result fail(int code, String message) {
        return new Result(code, message, null);
    }
}
